import React, { Component } from "react";
import { connect } from "react-redux";
import { createAddPersonAction } from "../../redux/actions/person";

class Person extends Component {
  addPerson = () => {
    const name = this.nameNode.value;
    const age = this.ageNode.value;
    const personObj = {
      id: Date.now(),
      name,
      age,
    };
    this.props.addPerson(personObj);
    this.nameNode.value = "";
    this.ageNode.value = "";
  };
  render() {
    return (
      <div>
        <h2>我是Person组件</h2>
        <input ref={(e) => (this.nameNode = e)} placeholder="输入姓名"></input>
        <input ref={(e) => (this.ageNode = e)} placeholder="输入年龄"></input>
        <button onClick={this.addPerson}>添加Person</button>
        <ul>
          {this.props.personList.map((item) => (
            <li key={item.id}>
              姓名：{item.name}，年龄：{item.age}
            </li>
          ))}
        </ul>
        <h3>Count组件求和为：{this.props.count}</h3>
      </div>
    );
  }
}

export default connect(
  (state) => ({ personList: state.personList, count: state.count }),
  {
    addPerson: createAddPersonAction,
  }
)(Person);
